package com.jrx.anytxn.param.job.investor.file.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.config.InvestorFileConfig;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrInvestorTable;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;


public class FileListener implements JobExecutionListener {

    private static final Logger logger = LoggerFactory.getLogger(FileListener.class);

    public FileListener(IOrganizationTableService organizationTableService, InvestorFileConfig investorFileConfig,
                        List<PrInvestorTable> investorList, String fileType) {
        this.organizationTableService = organizationTableService;
        this.investorFileConfig = investorFileConfig;
        this.investorList = investorList;
        this.fileType = fileType;
    }


    private IOrganizationTableService organizationTableService;

    private InvestorFileConfig investorFileConfig;

    private List<PrInvestorTable> investorList;

    private String fileType;

    @Override
    public void beforeJob(JobExecution jobExecution) {
        String reportingPath = investorFileConfig.getInvestorFilePath();
        String fileName = investorFileConfig.getInvestorFileName();

        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(Constant.DEFAULT_ORG_NUMBER);
        String toDay = DateUtils.format(org.getToday(), "yyyyMMdd");
        if(CollectionUtils.isEmpty(investorList)){
            return;
        }

        for (PrInvestorTable prInvestorTable : investorList) {
            String investorId = prInvestorTable.getInvestorId();
            if(Constant.DEFAULT_INVESTOR_ID.equals(investorId)){
                continue;
            }

            String collectionFileName = fileType + "_" + toDay + "_" + fileName + ".txt";

            String filePath = reportingPath +File.separator+ org.getTenantId() + File.separator + investorId + File.separator + toDay + File.separator;

            Path reportingFile = Paths.get(filePath + collectionFileName);
            Path pFile = Paths.get(filePath);

            if (!reportingFile.toFile().exists()) {
                //判断文件夹是否存在
                if (!pFile.toFile().isDirectory()) {
                    //创建文件夹
                    try {
                        Files.createDirectories(pFile);
                    } catch (IOException e) {
                        logger.error("创建目录失败", e);
                    }
                }
            } else {
                try {
                    Files.delete(reportingFile);
                } catch (IOException e) {
                    logger.error("文件{}删除失败", collectionFileName, e);
                }
            }

            //创建文件
            try {
                Files.createFile(reportingFile);
            } catch (IOException e) {
                logger.error("创建文件失败", e);
            }
        }
    }

    @Override
    public void afterJob(JobExecution jobExecution) {

    }
}
